package com.rjwh.dingdong.client.util;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.example.whrjwhbase.R;

public class AudioUtil {

	// 声音播放相关的变量
	public static MediaPlayer mediaPlayer;
	private static ImageView lastAudioLoadingImageView = null;
	public static int lastAudioFileIndex = -1; // 上一次播放的音频文件的index

	/**
	 * @Fields isSame : 解决double 点击同意语音时有可能还会继续播放的问题
	 */
	private static boolean isSame = false;

	public static void playAudio(String audioFileName) {
		// 检查入参
		if (null == audioFileName || audioFileName.length() <= 0) {
			return;
		}
		isSame = false;
		// 创建播放器
		if (null == mediaPlayer) {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.reset();
		}

		isSame = true;
		stopCurrentAudioPlaying();
		// 播放完成后的回调
		OnCompletionListener onCompletionListener = new OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {

				// 停止播放器
				if (mediaPlayer != null && mediaPlayer.isPlaying()) {
					mediaPlayer.stop();
				}
				// 释放资源
				if (mediaPlayer != null) {
					mediaPlayer.release();
					mediaPlayer = null;
				}
				lastAudioFileIndex = -1;
				System.gc();
			}
		};
		// 开始播放
		try {
			// 延迟 250 毫秒开始播放，以便刷新进度线程能够正常退出
			Thread.sleep(250);
			mediaPlayer.reset();
			mediaPlayer.setDataSource(audioFileName);
			mediaPlayer.setLooping(false);
			mediaPlayer.setOnCompletionListener(onCompletionListener);
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					if (isSame) {
						isSame = false;
						return;
					}
					mediaPlayer.start();
				}
			});
			mediaPlayer.prepareAsync();

		} catch (Exception e) {
			e.printStackTrace();

		}

	}

	public static void playAudio(String audioFileName, final ImageView imageView) {
		// 检查入参
		if (null == audioFileName || audioFileName.length() <= 0
				|| null == imageView) {
			return;
		}
		isSame = false;
		// 创建播放器
		if (null == mediaPlayer) {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.reset();
		}

		// 获得当前播放印迹的下标
		final int currentAudioFileIndex = (Integer) imageView.getTag();

		// 是否要停止当前播放
		if (currentAudioFileIndex == lastAudioFileIndex) {
			isSame = true;
			stopCurrentAudioPlaying();
			stopLodingVoice(imageView);
			return;
		}
		stopCurrentAudioPlaying();
		// 停止 之前的loading
		stopLodingVoice(lastAudioLoadingImageView);
		// 播放完成后的回调
		OnCompletionListener onCompletionListener = new OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				stopLodingVoice(imageView);
				// 停止播放器
				if (mediaPlayer != null && mediaPlayer.isPlaying()) {
					mediaPlayer.stop();
				}
				// 释放资源
				if (mediaPlayer != null) {
					mediaPlayer.release();
					mediaPlayer = null;
				}

				lastAudioFileIndex = -1;
				System.gc();

			}
		};

		lastAudioFileIndex = currentAudioFileIndex;
		lastAudioLoadingImageView = imageView;

		// 开始播放
		try {
			// 延迟 250 毫秒开始播放，以便刷新进度线程能够正常退出
			Thread.sleep(250);

			mediaPlayer.reset();
			mediaPlayer.setDataSource(audioFileName);
			mediaPlayer.setLooping(false);
			mediaPlayer.setOnCompletionListener(onCompletionListener);

			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					if (isSame) {
						isSame = false;
						return;
					}
					mediaPlayer.start();
					if (mediaPlayer != null && mediaPlayer.isPlaying()) {
						startLoadingVoice(imageView);
					}
				}
			});
			mediaPlayer.prepareAsync();

		} catch (Exception e) {
			e.printStackTrace();
			stopLodingVoice(imageView);

		}
	}

	public static void stopCurrentAudioPlaying() {

		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
			stopLodingVoice(lastAudioLoadingImageView);
		}
		lastAudioFileIndex = -1;
	}

	/**
	 * @Title: startLoadingVoice
	 * @Description: 开始loading,在正式播放前会loading加载
	 * @param @param imageView
	 * @return void
	 * @throws
	 * @history 1.YYYY-MM-DD author: description:
	 */
	private static void startLoadingVoice(ImageView imageView) {
		if (imageView != null) {
			if (imageView.getScaleType() == ScaleType.FIT_START) {
				imageView.setImageResource(R.drawable.left_animation);

			} else {
				imageView.setImageResource(R.drawable.right_animation);
			}
			AnimationDrawable drawable = (AnimationDrawable) imageView
					.getDrawable();
			if (drawable != null) {
				drawable.start();
			}
		}
	}

	/**
	 * @Title: stopLodingVoice
	 * @Description: 停止loading
	 * @param @param imageView
	 * @return void
	 * @throws
	 * @history 1.YYYY-MM-DD author: description:
	 */
	private static void stopLodingVoice(ImageView imageView) {
		if (imageView != null) {
			try {
				AnimationDrawable drawable = (AnimationDrawable) imageView
						.getDrawable();
				if (drawable != null) {
					drawable.stop();
				}
			} catch (Exception e) {
			}
			imageView.clearAnimation();
			if (imageView.getScaleType() == ScaleType.FIT_START) {
				imageView.setImageResource(R.drawable.skin_aio_ptt_action_l_3);
			} else {
				imageView.setImageResource(R.drawable.skin_aio_ptt_action_r_3);
			}
		}
	}

}
